package com.wwtech.epp.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.ninjia.ninjiabaselibrary.utils.SpUtil;
import com.wwtech.epp.R;
import com.wwtech.epp.application.AppConfig;
import com.wwtech.epp.base.BaseActivity;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.ui.activity.login.LoginActivity;
import com.wwtech.epp.ui.activity.mine.component.DaggerReSetPasswordComponent;
import com.wwtech.epp.ui.activity.mine.contract.ReSetPasswordContract;
import com.wwtech.epp.ui.activity.mine.module.ReSetPasswordModule;
import com.wwtech.epp.ui.activity.mine.presenter.ReSetPasswordPresenter;
import com.wwtech.epp.utils.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.mine
 * @Description: $description
 * @date 2017/09/05 14:27:50
 */

public class ReSetPasswordActivity extends BaseActivity implements ReSetPasswordContract.View {

    @Inject
    ReSetPasswordPresenter mPresenter;
    @BindView(R.id.old_password)
    EditText oldPassword;
    @BindView(R.id.new_password)
    EditText newPassword;
    @BindView(R.id.re_new_password)
    EditText reNewPassword;
    @BindView(R.id.bt_confirm)
    Button btConfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_reset_password);
        ButterKnife.bind(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void initData() {
        setTitle("修改密码");
    }

    @Override
    protected void setupActivityComponent() {
        DaggerReSetPasswordComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .reSetPasswordModule(new ReSetPasswordModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(ReSetPasswordContract.ReSetPasswordContractPresenter presenter) {
        mPresenter = (ReSetPasswordPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public void onReSetPasswordSuccess() {
        ToastUtil.displayShortToast("密码修改成功，请用新密码重新登录");
        SpUtil.putString(this, ContainValue.LOGINPHONE, "");
        SpUtil.putString(this, ContainValue.LOGINPASSWORD, "");
        startActivity(new Intent(this, LoginActivity.class));
    }

    @OnClick(R.id.bt_confirm)
    public void onViewClicked() {
        if (!TextUtils.isEmpty(oldPassword.getText().toString().trim())) {
            if (!oldPassword.getText().toString().trim().equals(SpUtil.getString(this, ContainValue.LOGINPASSWORD, ""))) {
                ToastUtil.showShort("旧密码错误");
                return;
            }
            if (!TextUtils.isEmpty(newPassword.getText().toString().trim())) {
                if (!TextUtils.isEmpty(reNewPassword.getText().toString().trim())) {
                    if (!newPassword.getText().toString().trim().equals(reNewPassword.getText().toString().trim())) {
                        ToastUtil.showShort("两次输入的密码不一致");
                        return;
                    }
                    //逻辑
                    Map<String, String> map = new HashMap<>();
                    map.put("uid", ContainValue.userLogin.getData().getUserId() + "");
                    map.put("oldpwd", oldPassword.getText().toString().trim());
                    map.put("newpwd", newPassword.getText().toString().trim());
                    mPresenter.reSetPassword(map);
                } else {
                    ToastUtil.showShort("新密码不能为空");
                }
            } else {
                ToastUtil.showShort("新密码不能为空");
            }
        } else {
            ToastUtil.showShort("旧密码不能为空");
        }
    }
}